//
//  YXMachinesBottomView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/28.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXMachinesBottomView.h"
#import "YXSearchBar.h"
#import "YXUserInfoModel.h"
@interface YXMachinesBottomView ()<UIPickerViewDataSource,UIPickerViewDelegate,UISearchBarDelegate>
@property (strong, nonatomic) IBOutlet UIButton *confirmBtn;
@property (nonatomic ,strong) UIPickerView *pickerView;
/** 搜索框 */
@property (nonatomic ,strong) YXSearchBar *searchBar;

@property (nonatomic ,strong) UIImageView *image;


@end

@implementation YXMachinesBottomView


- (void)setListArr:(NSArray *)listArr {
    _listArr = listArr;
    
    [self.pickerView reloadAllComponents];
}

- (void)setIndex:(NSString *)index {
    _index = index ;
    [self.pickerView selectRow:[self.index integerValue] inComponent:0 animated:YES];
}

- (YXSearchBar *)searchBar {
    if (!_searchBar) {
        _searchBar = [[YXSearchBar alloc]initWithFrame:CGRectZero];
        _searchBar.layer.cornerRadius = 20;
        _searchBar.layer.masksToBounds = YES;
        _searchBar.layer.borderWidth = 0.5;
        _searchBar.layer.borderColor = [UIColor grayColor].CGColor;
        _searchBar.delegate = self;
        _searchBar.frame = CGRectMake(40, 20, kScreenWidth - 80, 40);
        _searchBar.keyboardType = UIKeyboardTypeNumberPad;
    }
    return _searchBar;
}

-(instancetype)initWithFrame:(CGRect)frame withDataArray:(NSArray *)dataArray{
    
    if (self = [super initWithFrame:frame]) {
        // 从xib中找到我们定义的view
        NSArray *viewArray = [[NSBundle mainBundle]loadNibNamed:@"YXMachinesBottomView" owner:self options:nil];
        self = viewArray[0];
        self.frame = frame;
        self.backgroundColor = [UIColor whiteColor];
        self.layer.borderWidth = 1.0f;
        self.layer.borderColor = color_lineColor.CGColor;
        self.confirmBtn.layer.cornerRadius = 10.0f;
        self.confirmBtn.layer.masksToBounds = YES;
        
        [self setup];
    }
    return self;
    
}

- (void)setup {
    
    [self addSubview: self.searchBar];
    UIPickerView *picker = [[UIPickerView alloc] initWithFrame:(CGRectMake((kScreenWidth - 240)/2, self.searchBar.H + self.searchBar.Y + 20, 240, 100))];
    picker.backgroundColor = [UIColor whiteColor];
    picker.dataSource = self;
    picker.delegate = self;
    [self addSubview:picker];
    self.pickerView = picker;
    
    UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"椭圆"]];
    [picker addSubview:image];
    [image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(picker.mas_centerY);
        make.left.equalTo(self.mas_left).offset(100);
        [image sizeToFit];
    }];
    
}

- (IBAction)confirmBtnAction:(UIButton *)sender {
    if (self.selectBtnClickBlock) {
        self.selectBtnClickBlock(self.agentid);
    }
}

#pragma mark - UISearchBar Delegate
/**
 点击搜索框的代理方法
 
 @param searchBar 搜索按钮
 */
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar{
    
    [searchBar resignFirstResponder];
    if (self.inputPhoneNumber) {
        self.inputPhoneNumber(searchBar.text);
    }
    
}
#pragma mark - UIPickView的代理方法
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView {
    
    return 1;
}
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component {
    return self.listArr.count;
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    YXUserInfoModel *model = self.listArr[row];
    return model.name;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component {
    
    YXUserInfoModel *model = self.listArr[row];
    self.agentid = model.ID;
    NSLog(@"%@%@",model.name,model.ID);
    
}

// 每列宽度
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component {
    
    return 150;
    
}

@end
